package com.mms.entity.tag;

import com.mms.entity.dao.HibernateDao;
import com.mms.entity.dao.MybatisDao;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.DynamicAttributes;
import java.util.*;

/**
 * Created by dangz on 2017-5-23.
 */
public class ListTag extends BodyTagSupport implements DynamicAttributes {
    private static final Logger logger = LoggerFactory.getLogger(ListTag.class);
    private static final long serialVersionUID = -3007463448279167711L;
    private String sql;
    private String hql;
    private String statement;
    private final Map<String, Object> map = new HashMap();
    private int row;
    private String id;
    private String index = "index";
    private Iterator<?> iter;
    private int i = 0;

    public ListTag() {
    }

    public String getSql() {
        return this.sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public int getRow() {
        return this.row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public void setDynamicAttribute(String uri, String localName, Object value) throws JspException {
        this.map.put(localName, value);
    }

    public int doStartTag() throws JspException {
        try {
            List list = new ArrayList();
            ApplicationContext appCtx = ContextLoader.getCurrentWebApplicationContext();
            HibernateDao hibernateDao;
            if(StringUtils.isNotEmpty(this.hql)) {
                hibernateDao = (HibernateDao)appCtx.getBean("hibernateDao");
                if(this.row > 0) {
                    list = hibernateDao.find(0, this.row, this.hql, new Object[0]);
                } else {
                    list = hibernateDao.find(this.hql, new Object[0]);
                }
            } else if(StringUtils.isNotEmpty(this.sql)) {
                hibernateDao = (HibernateDao)appCtx.getBean("hibernateDao");
                Query query = hibernateDao.createSQLQuery(this.sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
                if(this.row > 0) {
                    query.setFirstResult(0);
                    query.setMaxResults(this.row);
                }

                list = query.list();
            } else if(StringUtils.isNotEmpty(this.statement)) {
                MybatisDao ibatisDao = (MybatisDao)appCtx.getBean("ibatisDao");
                list = ibatisDao.queryForList(this.statement, this.map);
            }

            if(((List)list).isEmpty()) {
                return 0;
            } else {
                int size = ((List)list).size();
                this.iter = ((List)list).iterator();
                if(this.iter.hasNext()) {
                    this.pageContext.setAttribute(this.id, this.iter.next());
                }

                this.pageContext.setAttribute(this.id + "_" + this.index, Integer.valueOf(this.i++));
                this.pageContext.setAttribute(this.id + "_size", Integer.valueOf(size));
                return 1;
            }
        } catch (Exception var5) {
            logger.error("", var5);
            return 0;
        }
    }

    public void doInitBody() throws JspException {
        super.doInitBody();
    }

    public int doAfterBody() throws JspException {
        if(this.iter.hasNext()) {
            this.pageContext.setAttribute(this.id, this.iter.next());
            this.pageContext.setAttribute(this.id + "_" + this.index, Integer.valueOf(this.i++));
            return 2;
        } else {
            return 0;
        }
    }

    public int doEndTag() throws JspException {
        return 6;
    }

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHql() {
        return this.hql;
    }

    public void setHql(String hql) {
        this.hql = hql;
    }

    public String getIndex() {
        return this.index;
    }

    public void setIndex(String index) {
        this.index = index;
    }

    public String getStatement() {
        return this.statement;
    }

    public void setStatement(String statement) {
        this.statement = statement;
    }
}
